package site.jjqzplucky.gateway.lib;
import cn.dev33.satoken.stp.StpInterface;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import site.jjqzplucky.gateway.mapper.PermissionMapper;
import site.jjqzplucky.gateway.mapper.RoleMapper;
import site.jjqzplucky.gateway.service.RedisService;
import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class StpInterfaceImpl implements StpInterface {

    private final RoleMapper roleMapper;
    private final PermissionMapper permissionMapper;
    private final RedisService redisService;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> list;
        if(redisService.getList("PERMISSION_LIST").isEmpty()){
            list = permissionMapper.getListByUserId(Long.valueOf((String) loginId));
            redisService.setList("PERMISSION_LIST", list);
        }
        return redisService.getList("PERMISSION_LIST");
    }


    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> list;
        if(redisService.getList("ROLE_LIST").isEmpty()){
            list = roleMapper.getListByUserId(Long.valueOf((String) loginId));
            redisService.setList("ROLE_LIST", list);
        }
        return redisService.getList("ROLE_LIST");
    }
}
